﻿var selID = "";

//$(document).ready(function () {
//    loadTechnicians();
//})

function fnLoadAddUser() {
    $('#formCustomerInfo').show();
    $('#formCustomerInfo').dialog({
        width: 500,
        modal: true,
        title: "Add New User",
        open: function () {
            $('#txtUserEmail').val('');
            $('#txtUserPassword').val('');
            $('#txtUserName').val('');
            $('#txtUserPhone').val('');
        }
    });
}
function addTechnician() {
    $('#addTechnicianContainer').show();
    $('#addTechnicianContainer').dialog({
        width: 500,
        height: 330,
        modal: true,
        title: "Add New Technician",
        open: function () {
            $('#txtTechName').val('');
            $('#txtTechMobile').val('');
            $('#txtTechAddress').val('');
        }
    });
}
function fnAddTechnician() {
    var name = $('#txtTechName').val().trim();
    var mobile = $('#txtTechMobile').val().trim();
    var address = $('#txtTechAddress').val().trim();
    if (name.length == 0 || mobile.length == 0 || address.length == 0) {
        $('#alertDiv').show();
        return false;
    }
    else {
        var params = { 'tName': name, 'tMobile': mobile, 'tAddress': address };
        var containerDIV = $('#addTechnicianContainer');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../_services/addTechnician.ashx", params, 'html', fnTechnicianCreated, '', '', opts);
    }
}
function fnTechnicianCreated(response) {
    $('#addTechnicianContainer').dialog('close');
    if (response == "y") {
        loadTechnicians();
    }
    else {

    }
}
function loadTechnicians() {
    var parentDiv = $('#techniciansListContainer');
    var moduleName = "../../_modules/ucTechnicians.ascx";
    var _params = '&z=';
    var opts = { 'onsucess': '', 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function closeAddUserPopup() {
    $(':input', '.addCustomerFormContainer')
        .not(':button, :submit, :reset, :hidden')
        .val('');
    $('#formCustomerInfo').dialog('close');
}

function createUser() {
    //alert("create user");
    $('#btnCreateUser').removeClass('adminColorButton').addClass('adminGrayButton');
    $('#btnCreateUser').removeAttr('onclick');
    var userEmail = $('#txtUserEmail').val();
    var userPassword = $('#txtUserPassword').val();
    var userName = $('#txtUserName').val();
    var userPhone = $('#txtUserPhone').val();
    //alert(userEmail + "," + userName + "," + userPassword + "," + userPhone);
    var params = { 'uEmail': userEmail, 'uPassword': userPassword, 'uName': userName, 'uPhone': userPhone };
    var containerDIV = $('#formCustomerInfo');
    var opts = { 'isLoadingGif': true, 'container': containerDIV };
    MakeJAjaxCall("../_services/createUser.ashx", params, 'html', fnUserCreated, '', '', opts);
}
function fnUserCreated(response) {
    //alert(response);
    if (response == 'y') {
        $('#btnCreateUser').removeClass('adminGrayButton').addClass('adminColorButton');
        $('#btnCreateUser').attr('onclick', 'createUser();');
        $(':input', '.addCustomerFormContainer')
        .not(':button, :submit, :reset, :hidden')
        .val('');
        $('#formCustomerInfo').dialog('close');
        genericInfoAlert("User created successfully!");
        loadUCUsers();
    }
    else {

    }
}
function deleteUser(obj) {
    //alert($(obj).attr('id'));
    var msg = 'Are you sure you want to delete the user?';
    var paramstoGeneral = { 'height': 70, 'width': 400, 'opacity': 50, 'resizable': false, 'modal': true, 'parentDiv': 'divInfoDialog', 'module': '', 'paramsforModule': '', 'paramforfunction': obj, 'type': 'info', 'msg': msg, 'yesclick': fnConfirmDelete, 'noclick': fnDialogNoClick, 'okclick': '' };
    fnGeneralDialog(paramstoGeneral);
    selID = $(obj).attr('id');
}
function fnConfirmDelete(obj) {
    var userid = $(obj).attr('id');
    var params = { 'uid': selID };
    var containerDIV = $(window);
    var opts = { 'isLoadingGif': true, 'container': containerDIV };
    MakeJAjaxCall("../_services/deleteUser.ashx", params, 'html', fnUserDeleted, '', '', opts);
}

function fnDeleteTechnician(obj) {
    var msg = 'Are you sure you want to delete the technician?';
    var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { fnDeleteTech($(obj).attr('id')); $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': true };
    genAlertPOPUP(opts);
}

function fnDeleteTech(techid) {
    var techid = techid;
    var params = { 'techid': techid };
    var containerDIV = $(window);
    var opts = { 'isLoadingGif': true, 'container': containerDIV };
    MakeJAjaxCall("../_services/deleteTechnician.ashx", params, 'html', fnTechDeleteSuccess, '', '', opts);
}
function fnTechDeleteSuccess(response) {
    if (response == "y") {
        loadTechnicians();
    }
    else {
        var msg = 'Error: Unable to save category name';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
function fnDialogNoClick() {
    $('#divInfoDialog').dialog('close');
}
function loadUCUsers() {
    var parentDiv = $('#repeaterContainer');
    var moduleName = "../../_modules/ucUsers.ascx";
    var _params = '&z=';
    var opts = { 'onsucess': incPage, 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function incPage() {

}

function fnUserDeleted(response) {
    //alert(response);
    if (response == 'y') {
        $('#btnCreateUser').removeClass('adminGrayButton').addClass('adminColorButton');
        $('#btnCreateUser').attr('onclick', 'createUser();');
        $(':input', '.addCustomerFormContainer')
        .not(':button, :submit, :reset, :hidden')
        .val('');
        $('#formCustomerInfo').dialog('close');
        genericInfoAlert("User Deleted!");
        loadUCUsers();
    }
    else {

    }
}